<?php

/**
 * This is the model class for table "{{participant}}".
 *
 * The followings are the available columns in table '{{participant}}':
 * @property integer $id
 * @property string $name
 * @property string $image
 * @property string $description
 * @property string $position
 * @property integer $age
 * @property string $formation
 * @property string $nationality
 * @property string $branche
 * @property string $sector
 * @property string $location
 * @property string $type
 */
class Participant extends CActiveRecord {


    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{participant}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name', 'required'),
            array('age', 'numerical', 'integerOnly' => true),
            array('image, description, position, formation, nationality, branche, sector, location, type', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('image', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'update'), 
            array('image', 'length', 'max'=>255, 'on'=>'insert,update'),
            array('id, name, image, description, position, age, formation, nationality, branche, sector, location, type', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'name' => 'Nome da empresa ou empresário',
            'image' => 'Foto',
            'description' => 'Descrição',
            'position' => 'Cargo na empresa',
            'age' => 'Idade',
            'formation' => 'Formação',
            'nationality' => 'Nacionalidade',
            'branche' => 'Ramo de atividade da empresa',
            'sector' => 'Setor',
            'location' => 'Localização',
            'type' => 'Tipo',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('image', $this->image, true);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('position', $this->position, true);
        $criteria->compare('age', $this->age);
        $criteria->compare('formation', $this->formation, true);
        $criteria->compare('nationality', $this->nationality, true);
        $criteria->compare('branche', $this->branche, true);
        $criteria->compare('sector', $this->sector, true);
        $criteria->compare('location', $this->location, true);
        $criteria->compare('type', $this->type, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Participant the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }
    
    public function hashCodeLink(){
        return md5($this->name).md5($this->image);
    }

}
